package com.servlet;

import com.alibaba.fastjson.JSON;
import com.pojo.user;
import com.service.userService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/user_SelectAll")
public class user_SelectAll extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        userService u = new userService();

        List<user> users = u.userselectAll();
//        System.out.println(users.toString());
        //获取登录时的session---不登录导致这个空值后面的数据都没法正常展示
        HttpSession session = req.getSession();
        user user = (user)session.getAttribute("user");

        users.add(user);//xx欢迎您

        //转化为json格式
        String user_json = JSON.toJSONString(users);

        //设置响应格式
        res.setContentType("text/json;Charset=utf-8");
        //后端发JSON串，前端then里的响应数据是json数组
        res.getWriter().write(user_json);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
